const BaseMapper = require('./base-mapper')

class Crud{
    /**
     * @property BaseMapper mapper
     */
    mapper;

    async getList(params = {}){
        return this.success(await this.mapper.getList(params))
    }

    async getPageList(params = {}){
        let list = await this.mapper.getPageList(params)
        return this.success(list)
    }

    async getRecyclePageList(params = {}){
        params['recycle'] = 1
        return this.success(await this.mapper.getPageList(params))
    }

    async create(data){
        await this.mapper.create(data)
        return this.success()
    }

    async save(data){
        
        await this.mapper.save(data)
        return this.success()
    }

    async delete(data){
        await this.mapper.delete(data)
        return this.success()
    }

    async realDelete(data){
        await this.mapper.delete(data, true)
        return this.success()
    }

    async recovery(data){
        await this.mapper.recovery(data)
        return this.success()
    }


}

module.exports = {
    crud(Clazz){
        Object.getOwnPropertyNames(Crud.prototype).map(name => {
            if(name in Clazz.prototype){
                return
            }
            if (typeof Crud.prototype[name] === 'function' && name !== "constructor") {
                //ee注册路由时使用
                Clazz.prototype[name] = Crud.prototype[name]
                //ee路由回调时使用
                Clazz[name] = Crud.prototype[name]
            }
        })
    }
}
